package com.yuexun.dhdoctor.widget.model;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.bean.model.HelpFileData;
import com.yuexun.dhdoctor.baselistener.selectListener;
import com.yuexun.dhdoctor.widget.ActionSheetDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenss on 2017/7/31 0031.
 */

public class CreateSpinnerView {

    Context context;
    ArrayList<HelpFileData> dtlList;//配置详情列表
    List<HelpFileData> alreadyDtlList;
    boolean isBackfill;

    public CreateSpinnerView(Context context, ArrayList<HelpFileData> dtlList, List<HelpFileData> alreadyDtlList, boolean isBackfill){
        this.context = context;
        this.dtlList = dtlList;
        this.alreadyDtlList = alreadyDtlList;
        this.isBackfill = isBackfill;
    }

    /**
     * 方法描述：从布局中添加下拉框布局
     *
     * @param fileData
     * @return
     */
    public View createSpinnerView(final HelpFileData fileData) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        View view = LayoutInflater.from(context).inflate(R.layout.add_item_spinner_layout_01, null);//也可以从XML中加载布局
        view.setLayoutParams(lp);//设置布局参数

        TextView txt_title = (TextView) view.findViewById(R.id.txt_title);
        final TextView spinnerTxt = (TextView) view.findViewById(R.id.spinner_txt);
        final LinearLayout spinnerView = (LinearLayout) view.findViewById(R.id.spinner_view);

        final List<String> list1 = new ArrayList<>();
        for (HelpFileData da : fileData.getList()) {
            list1.add(da.getDtlName());
        }

        txt_title.setText(fileData.getDtlName());
        if(isBackfill){
            for(HelpFileData data : alreadyDtlList){
                if(data.getDtlId().equals(fileData.getDtlId())){
                    spinnerTxt.setText(data.getList().get(0).getDtlValue());
                }
            }
        }else{
            spinnerTxt.setText(list1.get(0));
        }

        final HelpFileData fileD = new HelpFileData();
        fileD.setDtlName(fileData.getDtlName());
        fileD.setShowType("4");

        ArrayList<HelpFileData> helpFileDatas = new ArrayList<>();
        HelpFileData f = new HelpFileData();
        f.setDtlName(spinnerTxt.getText().toString());
        f.setDtlValue(spinnerTxt.getText().toString());
        f.setShowType(fileData.getShowType());
        helpFileDatas.add(f);
        fileD.setList(helpFileDatas);

        spinnerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSpinnerDialog(list1, spinnerTxt, new selectListener() {
                    @Override
                    public void getBack(int position) {
                        fileD.getList().get(0).setDtlName(list1.get(position));
                        fileD.getList().get(0).setDtlValue(list1.get(position));
                    }
                });
            }
        });
        dtlList.add(fileD);
        return view;
    }

    /**
     * 方法描述：从布局中添加下拉框布局
     *
     * @param fileData
     * @return
     */
    public View createSpinner2View(final HelpFileData fileData) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        View view = LayoutInflater.from(context).inflate(R.layout.add_item_spinner_layout_02, null);//也可以从XML中加载布局
        view.setLayoutParams(lp);//设置布局参数

        TextView txt_title = (TextView) view.findViewById(R.id.txt_title);
        final TextView spinnerTxt = (TextView) view.findViewById(R.id.spinner_txt);
        final LinearLayout spinnerView = (LinearLayout) view.findViewById(R.id.spinner_view);
        final TextView spinner2Txt = (TextView) view.findViewById(R.id.spinner2_txt);
        final LinearLayout spinner2View = (LinearLayout) view.findViewById(R.id.spinner2_view);

        txt_title.setText(fileData.getDtlName());

        final List<String> list1 = new ArrayList<>();
        final List<String> list2 = new ArrayList<>();
        for (HelpFileData da : fileData.getList()) {
            list1.add(da.getDtlName());
        }
        for (HelpFileData da : fileData.getList().get(0).getList()) {
            list2.add(da.getDtlName());
        }

        if(isBackfill){
            for(HelpFileData data : alreadyDtlList){
                if(data.getDtlId().equals(fileData.getDtlId())){
                    spinnerTxt.setText(data.getList().get(0).getDtlValue());
                    spinner2Txt.setText(data.getList().get(0).getList().get(0).getDtlValue());
                }
            }
        }else{
            spinnerTxt.setText(list1.get(0));
            spinner2Txt.setText(list2.get(0));
        }

        final HelpFileData fileD = new HelpFileData();
        fileD.setDtlName(fileData.getDtlName());
        fileD.setShowType("4");

        ArrayList<HelpFileData> helpFileDatas2 = new ArrayList<>();
        HelpFileData f2 = new HelpFileData();
        f2.setDtlName(spinner2Txt.getText().toString());
        f2.setDtlValue(spinner2Txt.getText().toString());
        f2.setShowType(fileData.getShowType());
        helpFileDatas2.add(f2);

        ArrayList<HelpFileData> helpFileDatas = new ArrayList<>();
        HelpFileData f = new HelpFileData();
        f.setDtlName(spinnerTxt.getText().toString());
        f.setDtlValue(spinnerTxt.getText().toString());
        f.setShowType(fileData.getShowType());
        f.setList(helpFileDatas2);
        helpFileDatas.add(f);

        fileD.setList(helpFileDatas);

        final int[] sele = new int[1];
        spinnerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSpinnerDialog(list1, spinnerTxt, new selectListener() {
                    @Override
                    public void getBack(int position) {
                        sele[0] = position;
                        list2.clear();
                        for (HelpFileData da : fileData.getList().get(position).getList()) {
                            list2.add(da.getDtlName());
                        }
                        spinner2Txt.setText(list2.get(0));

                        fileD.getList().get(0).setDtlName(list1.get(position));
                        fileD.getList().get(0).setDtlValue(list1.get(position));
                        fileD.getList().get(0).getList().get(0).setDtlName(list2.get(0));
                        fileD.getList().get(0).getList().get(0).setDtlValue(list2.get(0));
                    }
                });
            }
        });
        spinner2View.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSpinnerDialog(list2, spinner2Txt, new selectListener() {
                    @Override
                    public void getBack(int position) {
                        fileD.getList().get(0).getList().get(0).setDtlValue(list2.get(position));
                        fileD.getList().get(0).getList().get(0).setDtlName(list2.get(position));
                    }
                });
            }
        });
        dtlList.add(fileD);
        return view;
    }

    private void showSpinnerDialog(final List<String> list1, final TextView spinnerTxt, final selectListener listener) {
        ActionSheetDialog actionSheetDialog;
        actionSheetDialog = new ActionSheetDialog(context).builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true);

        for (int i = 0; i < list1.size(); i++) {
            final int final1 = i;
            actionSheetDialog.addSheetItem(list1.get(i), ActionSheetDialog.SheetItemColor.Blue,
                    new ActionSheetDialog.OnSheetItemClickListener() {
                        @Override
                        public void onClick(int which) {
                            spinnerTxt.setText(list1.get(final1));
                            listener.getBack(final1);
                        }
                    });
        }
        actionSheetDialog.show();
        ;
    }

}
